#!/usr/local/bin/php

<html>

<head>
<?php require_once 'input.css'; 
      require_once 'input.mod'; ?>

<title>Patient&nbsp;Form</title>
</head>

<body>

<?php

   //constraints
   define('OPEN',8);
   define('LUNCH',11);
   define('CLOSE',5);
   define('Path','../admin/');
   define('Patient','Patients.dat');
   define('Time','Time.dat');

   //compute time based off timestamp
   $offset = date('d', time()) - $_GET['day'];
   $date = strtotime("-$offset day", $_GET['date']);

   //load data files
   eval('$Patients = array(' . file_get_contents(Path . Patient) . ');');
   eval('$Time = array(' . file_get_contents(Path . Time) . ');');

/**************************Post-Submission********************************/

   if($_POST['user_form']) {

      //check if radio field entered
      if(!$_POST['user_radio']) 
         $error .= "Error, no time selected<br/>\n";
 
      //check for valid names
      filtration($_POST['user_name']);     
      if(!preg_match("/^[a-z]+\ [a-z]+$/i",$_POST['user_name']))
         $error .= "Error: Please enter first and last name<br/>\n";

      //check for valid address
      filtration($_POST['user_address']);     
      if(!preg_match('/^[0-9]+\ [a-z\ \.]+$/i',$_POST['user_address']))
         $error .= "Error: Please enter a number and a street name. " .
                    "For example: 123 cherry rd.<br/>\n";

      //check city/state field
      filtration($_POST['user_state']);
      if(!preg_match('/^[a-z]+,?\ [a-z]{2}$/i',$_POST['user_state']))
         $error .= "Error: Please enter a city followed by state. " .
                    "For example: Dallas, Tx<br/>\n";

      //check zip field
      filtration($_POST['user_zip']);
      if(!preg_match('/^\d{5}$/',$_POST['user_zip']))
         $error .= "Error: Please enter a 5 digit zip code<br/>\n";

      //check phone field
      filtration($_POST['user_phone']);
      if(!preg_match('/^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/',$_POST
         ['user_phone']))
         $error .= "Error: Please enter phone number in format: " .
                    "###-###-####<br/>\n";

      //check email
      filtration($_POST['user_email']);
      if(!preg_match('/^\w+@\w+\.\w+$/',$_POST['user_email']))
         $error .= "Error: Please enter an email in format: " .
                    "email@here.com<br/>\n";

      //check purpose of visit
      filtration($_POST['user_purpose']);
      if(!preg_match('/[a-z]+/i',$_POST['user_purpose']))
         $error .= "Error: Please let us know the purpose of your " . 
                    "appointment<br/>\n";

/*****************************Write Data**********************************/

      //output success
      if(!$error) {

         //manipulate array data for patients personal info
         $newArr = array_slice($_POST,3,5);
         $varName = str_replace(' ','_',$_POST['user_name']);
         $str = "'{$varName}' => ";

         //new patient
         if($_POST['user_new']) 
            file_write($str,$newArr,Path,Patient);

         //don't trust the user. verify he is a new patient
         else {
            if(count($Patients) < 1) 
               file_write($str,$newArr,Path,Patient);
            else {
               foreach($Patients as $index => $values) 
                  if(strcasecmp($index,$varName) == 0) $tmp = true;
               if(!$tmp) file_write($str,$newArr,Path,Patient);
            }
         }

         //manipulate array data for scheduling info
         $newerArr['user_name'] = $varName;
         $newerArr['user_radio'] = $_POST['user_radio'];
         $newerArr['user_new'] = $_POST['user_new'];
         $newerArr['user_purpose'] = $_POST['user_purpose'];
         $str = "'" . date('mdy',$date) . '_' . $_POST['user_radio'] .
                 "' => ";
         file_write($str,$newerArr,Path,Time);
         

         //output process
         echo "<div ID=\"form\">\n\n";
         echo "<h1>Appointment Successfully Submitted for " . 
               date('l\, M d \, Y', $date) . "<br/>From: " .
               "{$_POST['user_radio']}</h1>\n\n";

         //backwards navigation
         echo "<form method=\"post\" action=\"start.cgi\"/>\n\n";
         echo "<input class=\"one\" type=\"submit\" name=\"return\" " .
               "value=\"Return to Previous Calendar\" />\n";

         //include timestamp in hidden field
         echo "<input type=\"hidden\" name=\"timestamp\" " .
               "value=\"{$_GET['date']}\" /><br/>\n";

         //conclude forms
         echo "</form>\n\n"; 
         echo "</div>\n\n";
         echo "</body>\n\n</html>\n\n";
         return;
      }

   }

/***************************Pre-Submission********************************/

   //print title
   echo "<div ID=\"form\">\n\n";
   echo "<h1>Select New Appointment Time for " . date('l\, M d \, Y',
         $date) . "</h1>\n\n";

   //backwards navigation
   echo "<form method=\"post\" action=\"start.cgi\"/>\n\n";
   echo "<input class=\"one\" type=\"submit\" name=\"return\" value=\"" .
         "Return to Previous Calendar\" />\n";

   //include timestamp in hidden field
   echo "<input type=\"hidden\" name=\"timestamp\" " .
         "value=\"{$_GET['date']}\" /><br/>\n";

   echo "</form>\n\n"; 

   //display errors
   if($error) echo "<p>\n{$error}</p>\n\n";

   //begin form to handle user input fields
   echo "<form method=\"post\">\n\n";

   //hidden field containing time position
   $time_pass = date('M d Y', $date);
   echo "<input type=\"hidden\" name=\"time\" value=\"$time_pass\"/>\n\n";

   //instruct patient
   echo "<table align=\"center\" border=\"1\" bgcolor=\"blue\"><tr>\n";
   echo "<tr><th colspan=\"4\">Select an available time bellow" .
         "</th></tr>\n";
   echo '<tr><th colspan="2">' . date('D', $_GET['day']) . 
         "</th></tr>\n";

   //handle times
   echo "<tr><td>\n";
   for($i=OPEN; $i < 12+CLOSE; $i++) {

      //intervals
      $time1 = $i % 12;
      if($time1 == 0) { 
         $time1 = 12;
         $time2 = 1;
      }
      else $time2 = $time1 + 1;

      //hour lunch
      if($time1 == LUNCH) continue;

      //print
      $holder[$i] = "{$time1}-{$time2}";
      echo "{$time1}-{$time2}<br/>\n";
   }
   echo "</td>\n";

   //generate buttons
   echo "<td class=\"radio\">\n";
   for($i=OPEN; $i < 12+CLOSE; $i++) {
 
      //intervals for new column
      $time1 = $i % 12;
      if($time1 == 0) {
         $time1 = 12;
         $time2 = 1;
      }
      else $time = $time +1;

      if($time1 == LUNCH) continue;

      //determine availability
      $avaiv = date('mdy',$date) . '_' . $holder[$i];
      if($Time[$avaiv]['user_radio'] == $holder[$i]) { 
         echo "<font class=\"break\" color=\"white\"><b>X</b></font>" .
               "<br/>\n";
      }
      else { 
         $mem = ($_POST['user_radio'] == $holder[$i]) ? 'checked' : '';
         echo "<input class=\"break\" type=\"radio\" name=" .
               "\"user_radio\" value=\"{$holder[$i]}\" $mem/><br/>\n";
      }
   }
   echo "</td>\n\n";


   //generate information fields
   echo "<td align=\"right\">\n";
   echo "Name: <br/>";
   echo "Address: <br/>";
   echo "City/State: <br/>";
   echo "Zip Code: <br/>";
   echo "Phone: <br/>"; 
   echo "Email: <br/>";
   echo "Purpose of Visit: <br/>"; 
   echo "New Patient?";
   echo "</td>\n\n";
   echo "<td>\n";

   //generate fields from function 
   echo field_create('user_name');
   echo field_create('user_address');
   echo field_create('user_state');
   echo field_create('user_zip');
   echo field_create('user_phone');
   echo field_create('user_email');
   echo field_create('user_purpose');
   $mem = ($_POST['user_new']) ? 'checked' : '';
   echo "<input type=\"checkbox\" name=\"user_new\" $mem/>";
   echo "</td>\n";

   //conclude form table and form
   echo "</tr>\n\n</table>\n\n";
   echo "<input class=\"one\" type=\"submit\" name=\"user_form\" " . 
         "value=\"Submit Appointment\" />\n";
   echo "</form>\n";
   echo "</div>\n\n";

?>

</body>

</html>

